''' 滴滴打灰后端应用服务 WebAPI '''
from flask import Flask
from flask import Flask, jsonify, redirect, url_for, request,send_from_directory 
import logging, requests
import sys, traceback, os, re
from flask_socketio import SocketIO, emit, Namespace
from flask_cors import CORS
from configparser import ConfigParser
from ums.umsview import ums
from bms.bmsview import bms
from cms.cmsview import cms
from rms.rmsview import rms
from wms.wmsview import wms
from com.comview import com

app = Flask(__name__)
CORS(app, resources={r'/*': {'origins': '*'}})
socketio = SocketIO(app, cors_allowed_origins ="*")

# UMS管理    
app.register_blueprint(ums)

# BMS管理
app.register_blueprint(bms)

# CMS管理
app.register_blueprint(cms)

# RMS管理
app.register_blueprint(rms)

# WMS管理
app.register_blueprint(wms)

# 共同管理
app.register_blueprint(com)

# 配置日志文件
logger = logging.getLogger('didiapp')
logging.basicConfig(filename='didiapplog.txt',filemode='a',level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s: %(message)s')

# 文件存储在服务器的resume目录下，下载文件
@app.route('/resume/<path:filename>')
def send_file(filename):
    # 指定文件存储的文件夹
    directory = 'resume'
    return send_from_directory(directory, filename, as_attachment=True)

# 图片文件存储在服务器的useravatar目录下，下载图片
@app.route('/useravatar/<path:filename>')
def send_img_file(filename):
    # 指定文件存储的文件夹
    directory = 'useravatar'
    return send_from_directory(directory, filename, as_attachment=True)

# 图片文件存储在服务器的companylogo目录下，下载图片
@app.route('/companylogo/<path:filename>')
def send_logo_file(filename):
    # 指定文件存储的文件夹
    directory = 'companylogo'
    return send_from_directory(directory, filename, as_attachment=True)

# 图片文件存储在服务器的companylicense目录下，下载图片
@app.route('/companylicense/<path:filename>')
def send_auth_file(filename):
    # 指定文件存储的文件夹
    directory = 'companylicense'
    return send_from_directory(directory, filename, as_attachment=True)

if __name__ == '__main__':
    # 创建解析器对象
    config = ConfigParser()
     
    # 读取配置文件
    config.read('config.ini')

    # 读取server部分的配置项
    server_port = config.get('server', 'port')
    server_host = config.get('server', 'host')

    # 服务启动
    socketio.run(
        app, host=server_host, port=server_port
    )
